package com.school.graduation.detailed.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.school.graduation.R;
import com.school.graduation.common.base.BaseActivity;
import com.school.graduation.model.Detailed;
import com.school.graduation.util.ToastUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ReplyActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.activity_bottom_dialogbox_layout)
    RelativeLayout layout;
    @Bind(R.id.activity_bottom_dialogbox_edit)
    EditText editText;
    @Bind(R.id.activity_bottom_dialogbox_send)
    TextView send;
    private String tid;
    private String cid;
    private String product;
    private String from;
    private Detailed.CommentsBean comments;
    @Override
    public int getContentView() {
        return R.layout.activity_bottom_dialogbox;
    }

    @Override
    public void initData() {
        initOnclickListener();
        setEditWatcher();
        comments = (Detailed.CommentsBean)getIntent().getSerializableExtra("data");
        tid = comments.getFrom().get_id();
        cid = comments.get_id();
        product = comments.getProduct();
        SharedPreferences sp = getApplicationContext().getSharedPreferences("user_cache", Context.MODE_MULTI_PROCESS |
                Activity.MODE_WORLD_WRITEABLE);
        from = sp.getString("id","");

    }

    private void initOnclickListener(){
        layout.setOnClickListener(this);
    }

    private void setEditWatcher(){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() != 0) {
                    final String content = s.toString();
                    send.setBackground(context.getResources().getDrawable(R.drawable.frame_red));
                    send.setTextColor(context.getResources().getColor(R.color.md_white_1000));
                    send.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            sendReply(content);
                        }
                    });
                }else {
                    send.setBackground(context.getResources().getDrawable(R.drawable.frame));
                    send.setTextColor(context.getResources().getColor(R.color.md_blue_grey_500));
                    send.setOnClickListener(null);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void sendReply(final String content){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient.Builder()
                        .connectTimeout(1000, TimeUnit.SECONDS)
                        .readTimeout(1000,TimeUnit.SECONDS)
                        .build();
                RequestBody body = new FormBody.Builder()
                        .add("product",product)
                        .add("from",from)
                        .add("content",content)
                        .add("tid",tid)
                        .add("cid",cid)
                        .build();
                Request request = new Request.Builder()
                        .url("http://123.206.20.29:3000/user/comment")
                        .post(body)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        ToastUtil.makeShortToast(context,"回复成功");
                        finish();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        ToastUtil.makeShortToast(context,"回复成功");
                        finish();
                    }
                });
            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.activity_bottom_dialogbox_send:
                break;
            case R.id.activity_bottom_dialogbox_edit:
                break;
            default:
                finish();
                break;
        }
    }
}
